﻿namespace FFF.Test.Jobs
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Net.Mail;
    using Abp.BackgroundJobs;
    using Abp.Dependency;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore;
    using Abp.Net.Mail;
    using Microsoft.EntityFrameworkCore;
    using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
    using FFF.Test.DataAccess.Framework;

    /// <summary>
    /// 发送邮件任务
    /// </summary>
    public class SendEmailJob : BackgroundJob<SendEmailJobArgs>, ITransientDependency
    {
        private readonly IDbContextProvider<MyDbContext> _dbContextProvider;

        private readonly IEmailSender _emailSender;

        /// <summary>
        /// Initializes a new instance of the <see cref="SendEmailJob"/> class.
        /// 构造函数
        /// </summary>
        /// <param name="dbContextProvider">数据库访问对象</param>
        /// <param name="emailSender">邮件对象</param>
        public SendEmailJob(
            IDbContextProvider<MyDbContext> dbContextProvider,
            IEmailSender emailSender)
        {
            this._dbContextProvider = dbContextProvider;
            this._emailSender = emailSender;
        }

        /// <summary>
        /// job执行方法
        /// </summary>
        /// <param name="args">参数</param>
        [UnitOfWork(isolationLevel: System.Transactions.IsolationLevel.ReadUncommitted)]
        public override void Execute(SendEmailJobArgs args)
        {
            var db = this._dbContextProvider.GetDbContext();

            MailMessage mail = new MailMessage();
            mail.Subject = args.Subject;
            mail.Body = args.Body;
            mail.IsBodyHtml = args.IsBodyHtml;
            args.Tos.ForEach(item => mail.To.Add(item));
            args.CCs.ForEach(item => mail.CC.Add(item));
            args.Attachments.ForEach(item => mail.Attachments.Add(new Attachment(item)));

            //mail.To.Add("mingjay100@163.com");
            //mail.To.Add("418146370@qq.com");
            //mail.CC.Add("4181463719999@qq.com");
            //mail.CC.Add("yudaming@whcewei.com");
            this._emailSender.Send(mail);
        }
    }

    /// <summary>
    /// job参数
    /// </summary>
    [Serializable]
    public class SendEmailJobArgs
    {
        /// <summary>
        /// 发送人
        /// </summary>
        public List<string> Tos { get; set; } = new List<string>();

        /// <summary>
        /// 抄送人
        /// </summary>
        public List<string> CCs { get; set; } = new List<string>();

        /// <summary>
        /// 附件
        /// </summary>
        public List<string> Attachments { get; set; } = new List<string>();

        /// <summary>
        /// 是否是html
        /// </summary>
        public bool IsBodyHtml { get; set; } = true;

        /// <summary>
        /// 右键标题
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 发送内容
        /// </summary>
        public string Body { get; set; }
    }
}
